package com.jt.tenant.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jt.tenant.R;
import com.jt.tenant.app.JTApplication;
//import com.jt.tenant.dialog.InsuranceDialog;
import com.jt.tenant.dialog.MyTimePopup;
import com.jt.tenant.popwindow.PicShowPopup;
import com.jt.tenant.widget.OnOffAgreementView;
import com.jt.tenant.widget.Tools;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by AD on 2016/5/12.
 */

public class InsInfoActivity extends JTActivity implements OnClickListener {

    private TextView title_name1, tv_ins_info_next;

    private LinearLayout lin_ins_call;

    private ImageView iv_wenhao_vin,iv_wenhao_engine;

    //投保人信息

    private EditText et_in_username;//姓名

    private EditText et_in_id_number;//身份证

    private EditText et_in_vincode;//VIN

    private EditText et_in_engine_number;//发动机号

    private EditText et_in_plate_number;//车牌号

    private OnOffAgreementView new_car;//新车未落牌

    //品牌、型号、排量、年款、变速挡

    private EditText tv_category_name;

    private TextView select_car;//选择车辆信息

    private LinearLayout lin_buy_year;

    private TextView et_gmnf;

    private String address = "哈尔滨";

    private String plate_num;

//    private InsuranceDialog insuranceDialog;

    private String BRAND, CS, CM, DM, TM, YEAR;

    private MyTimePopup timePopupWindow;

    private PicShowPopup mPop;

    private String id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ins_info);

        id = getIntent().getStringExtra("id");

        initView();

    }

    private void initView() {

        mPop = new PicShowPopup(this);

        (title_name1 = (TextView) findViewById(R.id.tv_title_name)).setText("投保信息");

        (tv_ins_info_next = (TextView)findViewById(R.id.tv_ins_info_next)).setOnClickListener(this);

        (lin_ins_call = (LinearLayout)findViewById(R.id.lin_ins_call)).setOnClickListener(this);

        (iv_wenhao_vin = (ImageView)findViewById(R.id.iv_wenhao_vin)).setOnClickListener(this);

        (iv_wenhao_engine = (ImageView)findViewById(R.id.iv_wenhao_engine)).setOnClickListener(this);

        timePopupWindow = new MyTimePopup(context, null, new Date(), MyTimePopup.Type.YEAR_MONTH_DAY) {

            private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");

            @Override
            public void onSubmit(Date date) {

                et_gmnf.setText(simpleDateFormat.format(date));

            }

        };

        lin_buy_year = (LinearLayout) findViewById(R.id.lin_buy_year);

        lin_buy_year.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                //先关闭软键盘
                View view = getWindow().peekDecorView();

                if (view != null) {

                    InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

                    inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);

                }

                timePopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

            }
        });

        Calendar calendar_before_70y = Calendar.getInstance();

        calendar_before_70y.set(Calendar.YEAR, calendar_before_70y.get(Calendar.YEAR) - 70);

        Calendar calendar_after_3m = Calendar.getInstance();

        calendar_after_3m.set(Calendar.MONTH, calendar_after_3m.get(Calendar.MONTH) + 3);

        et_in_username = (EditText) findViewById(R.id.et_in_username);

        et_in_id_number = (EditText) findViewById(R.id.et_in_id_number);

        new_car = (OnOffAgreementView) findViewById(R.id.new_car);

        et_in_vincode = (EditText) findViewById(R.id.et_in_vincode);

        et_in_engine_number = (EditText) findViewById(R.id.et_in_engine_number);

        et_in_plate_number = (EditText) findViewById(R.id.et_in_plate_number);

        tv_category_name = (EditText) findViewById(R.id.et_ins_brand);

        et_gmnf = (TextView) findViewById(R.id.tv_ins_info_gmnf);

        (select_car = (TextView) findViewById(R.id.tv_select_car)).setOnClickListener(this);

        if (JTApplication.jtPreferences.readCX().equals("null") || JTApplication.jtPreferences.readCX() == null) {

            tv_category_name.setText("");

        } else {

            tv_category_name.setText(JTApplication.jtPreferences.readCX());

        }

        et_in_vincode.setText(JTApplication.jtPreferences.readVincode());

        et_in_engine_number.setText(JTApplication.jtPreferences.readEngineCode());

        et_in_plate_number.setText(JTApplication.jtPreferences.readPlateNum());

        et_in_id_number.setText(JTApplication.jtPreferences.readDriverNum());

        et_gmnf.setText(JTApplication.jtPreferences.readRemark());

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_wenhao_vin:

                mPop.setType(0);

                mPop.show();

                break;

            case R.id.iv_wenhao_engine:

                mPop.setType(1);

                mPop.show();


                break;

            case R.id.tv_select_car:

                startActivityForResult(new Intent(InsInfoActivity.this, ChooseModelsActivity.class), 11);

                break;

            case R.id.tv_ins_info_next:

                onNext();

                break;

            case R.id.lin_ins_call:

                Tools.Call(this,"4006115616");

                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) { //resultCode为回传的标记，我在B中回传的是RESULT_OK
            case RESULT_OK:

                Bundle b = data.getExtras(); //data为B中回传的Intent

                BRAND = b.getString("BRAND");

                CS = b.getString("CS");

                CM = b.getString("CM");

                DM = b.getString("DM");

                TM = b.getString("TM");

                YEAR = b.getString("YEAR");

                tv_category_name.setText(CS + "  " + CM);

                break;
            default:
                break;
        }

    }

    private void onNext() {

        if (!Tools.isName(et_in_username.getText().toString())) {

            Toast.makeText(this, "请填写投保人姓名", Toast.LENGTH_SHORT).show();

        }  else if (!Tools.personIdValidation(et_in_id_number.getText().toString())) {

            Toast.makeText(this, "请填写投保人身份证号", Toast.LENGTH_SHORT).show();

        }  else if (!Tools.isPlateNO(et_in_plate_number.getText().toString()) && !new_car.isCheck()) {

            Toast.makeText(this, "请正确填写车牌号", Toast.LENGTH_SHORT).show();

        } else if (!Tools.isVinNO(et_in_vincode.getText().toString())) {

            Toast.makeText(this, "请正确填写17位车架号", Toast.LENGTH_SHORT).show();

        } else if (et_in_engine_number.getText().toString().equals("")) {

            Toast.makeText(this, "请正确填写发动机号", Toast.LENGTH_SHORT).show();

        } else if ( tv_category_name.getText().toString().equals("") ) {

            Toast.makeText(this, "请填写完整车辆信息", Toast.LENGTH_SHORT).show();

        } else if (et_gmnf.getText().toString().equals("")) {

            Toast.makeText(this, "请正确选择注册日期", Toast.LENGTH_SHORT).show();

        } else {

            if (new_car.isCheck()) {

                plate_num = "黑A*";

            } else {

                plate_num = et_in_plate_number.getText().toString();

            }

            Intent intent = new Intent(this, InsSelectActivity.class);

            Bundle mbundle = new Bundle();

            mbundle.putString("id",id);

            mbundle.putString("name", et_in_username.getText().toString().trim());

            mbundle.putString("id_number", et_in_id_number.getText().toString().trim());

            if(new_car.isCheck()){

                mbundle.putString("plate_number", "新车未上牌");

            }else {

                mbundle.putString("plate_number", et_in_plate_number.getText().toString().trim());

            }

            mbundle.putString("vin", et_in_vincode.getText().toString().trim());

            mbundle.putString("engine_number", et_in_engine_number.getText().toString().trim());

            mbundle.putString("category_name", tv_category_name.getText().toString().trim());

            mbundle.putString("gmnf", Tools.dataFour(et_gmnf.getText().toString().trim())+"000");

            intent.putExtras(mbundle);

            startActivity(intent);

        }
    }
}
